﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CallCenter.Model;

namespace CallCenter.Gui
{
    public partial class CustomerDetailsForm : Form
    {
        private Customer customer;
        private WGRUSEntities context;

        public CustomerDetailsForm()
        {
            InitializeComponent();
            context = new WGRUSEntities();
        }


        private void bt_SeekByPhoneNr_Click(object sender, EventArgs e)
        {
            int phoneNr;
            if (!int.TryParse(telefonNrTextBox.Text, out phoneNr))
            {
                MessageBox.Show("Telefonnummer skal være numerisk", "Fejl", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            customer = context.Customers.FirstOrDefault(c => c.TelefonNr == phoneNr);
            if (customer == null)
            {
                MessageBox.Show("Kunde findes ikke i databasen", "Info", MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
                return;
            }

            FillGui(customer);
        }


        private void bt_Reset_Click(object sender, EventArgs e)
        {
            Reset();
        }

        private void bt_Create_Click(object sender, EventArgs e)
        {
            customer = new Customer();
            customer.Oprettet = DateTime.Now;
            FillCustomer(customer, true);
            context.Customers.Add(customer);
            context.SaveChanges();

            MessageBox.Show("Kunden er oprettet", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }


        private void bt_Update_Click(object sender, EventArgs e)
        {
            FillCustomer(customer, false);
            //context.Entry(customer).State = EntityState.Modified;
            context.SaveChanges();
            MessageBox.Show("Rettelse gemt", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void bt_CreateOrder_Click(object sender, EventArgs e)
        {
            if (customer == null)
            {
                MessageBox.Show("Kunden skal være fundet", "Fejl", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Form f = new OrderCardForm(customer.TelefonNr);
            f.MdiParent = this.MdiParent;
            f.Show();
        }

        private void FillGui(Customer cust)
        {
            telefonNrTextBox.ReadOnly = true;
            telefonNrTextBox.Text = cust.TelefonNr.ToString();
            navnTextBox.Text = cust.Navn;
            gadeTextBox.Text = cust.Gade;
            byTextBox.Text = cust.By;
            emailTextBox.Text = cust.Email;
            postNrTextBox.Text = cust.PostNr.ToString();
        }

        private void FillCustomer(Customer cust, bool fillPhone)
        {
            int phoneNr;
            if (!int.TryParse(telefonNrTextBox.Text, out phoneNr))
            {
                MessageBox.Show("Telefonnummer skal være numerisk", "Fejl", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            int zipCode;
            if (!int.TryParse(postNrTextBox.Text, out zipCode))
            {
                MessageBox.Show("Postnummer skal være numerisk", "Fejl", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (fillPhone)
            {
                cust.TelefonNr = phoneNr;
                telefonNrTextBox.ReadOnly = true;
            }


            cust.Navn = navnTextBox.Text;
            cust.Gade = gadeTextBox.Text;
            cust.By = byTextBox.Text;
            cust.PostNr = zipCode;
            cust.Email = emailTextBox.Text;
        }

        private void Reset()
        {
            telefonNrTextBox.ReadOnly = false;
            telefonNrTextBox.Text = string.Empty;
            navnTextBox.Text = string.Empty;
            gadeTextBox.Text = string.Empty;
            byTextBox.Text = string.Empty;
            postNrTextBox.Text = string.Empty;
            emailTextBox.Text = string.Empty;
            customer = null;
        }


    }
}